/*自定义样式多选标签*/
//提供tag基本类
import 'package:flutter/cupertino.dart';
import 'package:frametrim/views/multipleChoice/listenerchoice/BaseSelectEntity.dart';
import 'package:frametrim/views/multipleChoice/select_choice_chip.dart';

class MultipleChoiceChip<T extends BaseSelectEntity> extends StatefulWidget {
  //标签数据
  late final List<T> dataList;

  //是否单选
  late bool isSingleChoice;

  //默认标签数据
  // late final List<T>? selectList;

  //选中回调事件
  late final Function(List<T>) onSelectionChanged;

  MultipleChoiceChip(
      {required this.dataList,
      // this.selectList,
      this.isSingleChoice = false,
      required this.onSelectionChanged});

  @override
  State<StatefulWidget> createState() => MultipleChoiceState();
}

class MultipleChoiceState<T extends BaseSelectEntity>
    extends State<MultipleChoiceChip> {
  late List<T> selectList = [];

  MultipleChoiceState();

  @override
  void initState() {
    super.initState();
    print('initState()');
    //进入后默认选中第一个
    selectList.add(widget.dataList[0] as T);
  }

  List<Widget> _buildChoiceList() {
    List<Widget> choice = [];
    widget.dataList.forEach((item) {
      choice.add(Container(
        padding: EdgeInsets.only(left: 10, right: 10, bottom: 5, top: 5),
        child: SelectChoiceChip(
          text: item.getTag(),
          selected: selectList.contains(item as T),
          onSelect: (selected) {
            setState(() {
              if (widget.isSingleChoice) {
                selectList.clear();
                selectList.add(item as T);
              } else {
                selectList.contains(item as T)
                    ? selectList.remove(item as T)
                    : selectList.add(item as T);
              }
              widget.onSelectionChanged(selectList);
            });
          },
        ),
      ));
      bool a = selectList.contains(item as T);
      print('aaa=' + a.toString() + "item==" + item.getTag());
    });
    return choice;
  }

  @override
  Widget build(BuildContext context) {
    return Wrap(
      children: _buildChoiceList(),
    );
  }
}
